/**
 * ClassName 部门管理控制器
 */
Ext.define("app.controller.AttendanceController",{
	
	extend:'Ext.app.Controller',
	init: function(){
		
		this.control({
			//申请加班或请假 弹窗
			'attendanceself button[action = apply]':{
				click:'apply',
			},
			//申请加班或请假 提交
			'attendanceapply button[action = submit]':{
				click:'applySubmit'
			},
			//签到模板下载
			'attendancelist button[action = download]':{
				click:'signDownload'
			},
			//签到信息导入 弹窗
			'attendancelist button[action = import]':{
				click:'importWindow'
			},
			//签到信息导入 上传Excel
			'uploadsign button[action = upload]':{
				click:'signImport'
			},
			//签到信息导入 提交
			'uploadsign button[action = addAll]':{
				click:'signAddAll'
			},
			//查看考勤申请 
			'attendancelist button[action = check]':{
				click:'attendCheck',
			},
			//查看考勤申请 
			'attendancestaff':{
				itemdblclick:'checkApply',
			},
			//考勤通过 
			'attendcheckapply button[action = pass]':{
				click:'passApply',
			},
			//考勤拒绝
			'attendcheckapply button[action = refuse]':{
				click:'refuseApply',
			},
		})
	},
	
	//批量添加员工签到信息 提交
	signAddAll:function(btn,e){
		//console.log(record);
		var hidden =Ext.getCmp('excelPath');
		var data= { 'path' : hidden.value };				
		//2.ajax后台操作
		var obj = {
				win : btn.up("window"),
				data : data,
				url : ROOT+'/index.php/Home/Attendance/signUpload',
				operate : 'window',
				record : Ext.getCmp('attendancelist'),
		};	
		console.log(obj.record);
		var addAllAjax = new myAjax(obj);
		addAllAjax.request();		
	},
	
	//签到信息导入 弹窗
	importWindow:function(grid,record){
		var win = Ext.widget('uploadsign');
		win.show();
	},
	
	//导入员工签到表 上传Excel
	signImport:function(btn, e, eOpts){
		var win = btn.up("window");
		var form = win.down('form');
		var file = form.down('filefield');
		var hidden =Ext.getCmp('excelPath');
		//var hidden = form.down('hidden');
		//file.emptyText = file.getValue();
		console.log(form);
		console.log(file);
		console.log(form.ownerCt);

/*		console.log(form);
		console.log(form);*/
        if(form.isValid()){
            form.submit({
                url: ROOT+'/index.php/Home/Staff/uploadExcel',
                waitMsg: 'Uploading your excel...',
                success: function(fp, o) {
                	//console.log(fp.responseText);
                	console.log(o.response.responseText);
                	
                var response =	Ext.decode(o.response.responseText);
                hidden.setValue(response.file);
                	//win.hide();
                   Ext.Msg.alert('Success',response.msg );
                },
            
            });
        }
	},
	
	//下载员工每日签到模板
	signDownload:function(grid,record){
		window.location=ROOT+'/index.php/Home/Attendance/downloadExcel';
	},
	
	//申请加班或请假 提交
	applySubmit:function(btn){
		var win =  btn.up('window');
		var form = win.down('form');
		
		var obj = {
				win : win,
				data :form.getValues() ,
				url : ROOT+'/index.php/Home/Attendance/apply',
				operate : 'window',				
		};
		if(form.getForm().isValid()){
			var ajax = new myAjax(obj);
			ajax.request();	
		}				
	},
	
	//申请加班或请假 弹窗
	apply:function(btn){
		var obj = {
				win : Ext.widget('attendanceapply'),
				url : ROOT+'/index.php/Home/Staff/find',
				operate : 'updateSelf',				
		};
		var ajax = new myAjax(obj);
		ajax.request();	
	},
	
	//点击左边导航 右边弹窗对应的面板
	attendCheck:function(btn, e){
		var panel = Ext.getCmp('center-grid');
		var grid=btn.ownerCt.ownerCt;
		var rows=grid.getSelectionModel().getSelection();
		if(rows.length != 1){
			Ext.Msg.alert('警告','只能选择一行');
			return false;
		}
		var data = {};
		Ext.Array.each(rows,function(record){
			data = record.getData();			
	
			var card ={
					title: data.name,				
					closable: true,
					//id:'dept'+data.id,
					xtype:'attendancestaff',			
				};						
		})
		data.start = 0;	
		var store = Ext.getCmp('attendcheck').getComponent('attendancestaff').getStore()
		store.load({params:data});//加载store时传参！！！！	
		store.on("beforeload",function(){
		    Ext.apply(store.proxy.extraParams, {sid:data.sid});
		});
	},	
	
	//双击审核员工考勤申请 弹窗
	checkApply:function(grid,record){
		var win = Ext.widget('attendcheckapply');
		console.log(record);
		win.down("form").loadRecord(record);
		win.show();
	},
	
	//考勤通过 提交
	passApply:function(btn,e,o){
		var win = btn.up("window");
		var form = win.down('form');
		var record = form.getRecord();				
		var	iid = record.get('iid');
		var operate = 'window';				
		var data = form.getValues();
		data.iid = iid;	
		data.flag = 1;	
		var obj = {
			    form : form,
			    record : record,
				win : win,
				data : data,
				url : ROOT+'/index.php/Home/Attendance/checkApl',
				operate : operate,
		};		
		var saveAjax = new myAjax(obj)		
		saveAjax.request();				
	},
	
	//考勤拒绝 提交
	refuseApply:function(btn,e,o){
		var win = btn.up("window");
		var form = win.down('form');
		var record = form.getRecord();				
		var	iid = record.get('iid');
		var operate = 'window';				
		var data = form.getValues();
		data.iid = iid;	
		data.flag = 2;
		var obj = {
			    form : form,
			    record : record,
				win : win,
				data : data,
				url : ROOT+'/index.php/Home/Attendance/checkApl',
				operate : operate,
		};		
		var saveAjax = new myAjax(obj)		
		saveAjax.request();				
	},
	
	views:[
	       'attendance.CheckApply',
	       'attendance.AttendCheck',
	       'attendance.Staff',
	       'attendance.Apply',
	       'attendance.List',
	       'attendance.Self',
	       'attendance.UploadSign',
	       ],
	stores:[
	        'attendance.ListStore',
	        'attendance.SelfStore',
	        'attendance.SelectStore',
	        'attendance.StaffStore',
	        'attendance.LeaveStore'
	       ],
	//models:['StaffModel'],
})